package com.huiyuanai.film.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.huiyuanai.film.entity.Movie;
import com.huiyuanai.film.entity.User;
import com.huiyuanai.film.lang.Result;
import com.huiyuanai.film.mapper.UserMapper;
import com.huiyuanai.film.query.MovieQuery;
import com.huiyuanai.film.query.UserQuery;
import com.huiyuanai.film.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 灰原哀
 * @since 2022-05-27
 */
@RestController
@RequestMapping("/film/user")
@Api(tags = "用户接口",description = "用户接口详细api描述")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("User:查询所有信息")
    @GetMapping("/all")
    public Result queryUserAll(){
        List<User> list = userService.list();
        return Result.succ("查询成功", list);
    }

    @ApiOperation("User:增加或修改")
    @PostMapping("/addUpdata")
    public Result addUpdata(@Valid @RequestBody User user){
        boolean b = userService.saveOrUpdate(user);
        if(b == true){
            return Result.succ("操作成功");
        }else{
            return Result.fail("操作失败");
        }
    }

    @ApiOperation("User:删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Integer id){
        boolean b = userService.removeById(id);
        if(b == true){
            return Result.succ("操作成功");
        }else{
            return Result.fail("操作失败");
        }
    }


    @ApiOperation("User:分页查询")
    @PostMapping("/userPage")
    public Result moviesPage(@RequestBody UserQuery userQuery){
        try {
            PageInfo<User> userPageInfo = userService.userPage(userQuery);
            return Result.succ("查询成功", userPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }
}
